// Creates a plane heading to the destination, places the group in it and ejects the group.
private ["_dest","_units","_flying_units","_flying_height","_dstart_pos","_halo_vehicle_type","_crew_start_pos","_vehicle","_spos","_cdir","_unit","_crew","_turn_pos"];
diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"halogroup.sqf","Start",_this];
_dest = _this select 0;
_units = list D_AI_HUT_TRIGGER;

_flying_units = [];
{
	if (isplayer _x) then {
		_flying_units set [count _flying_units, _x];
	};
} foreach _units;
_halo_vehicle_type = "C130J";
_flying_height = HALOJumpHeight + 300;

_dstart_pos = _dest;
_dstart_pos = [_dest, 3500,2500] call XfGetRanPointCircleAir;
_dest set [2, _flying_height];

// We create a group for the crew and we create a vehicle of the type defined in x_taxi_aircraft.
// We make a civilian pilot to avoid AI enhancement scripts to pick it up and change its behavior. Plus it's equivalent to making it "Captive"
_crew_start_pos = d_pos_ai_spawn select 0;
_grp = [d_own_side] call x_creategroup;
_spos = [_dstart_pos select 0, _dstart_pos select 1, (2 * _flying_height)];
_cdir = [_spos, _dest] call XfDirTo;

_turn_pos = [(700 * (sin _cdir) + (_dstart_pos select 0)),(700 * (cos _cdir) + (_dstart_pos select 1)), _flying_height];

_vehicle = createVehicle [_halo_vehicle_type, _spos, [], 0, "FLY"];
_vehicle setPos _spos;
_vehicle setDir _cdir;
_vehicle setVelocity [120 * (sin _cdir), 120 * (cos _cdir), 0];
_vehicle setVariable ["asr_ai_sys_aiskill_exclude", true];
_vehicle setVariable ["zeu_AIBypass", true];
_vehicle allowDamage false;
_grp addVehicle _vehicle;
_crew_type = switch (d_own_side) do {
		case west: {"USMC_Soldier_Pilot"};
		case east: {"RU_Soldier_Pilot"};
		default {"RU_Pilot"};
	};
_unit = [_crew_type, _grp, _crew_start_pos] call XfCreateUnit;
// _unit = _grp createUnit [_crew_type, _spos, [], 0, "NONE"];
while {vehicle _unit == _unit} do {
	_unit setPos (getPos _vehicle);
	_unit moveinDriver _vehicle;
};
_crew = crew _vehicle;
_grp selectLeader (commander _vehicle);
_grp setCombatMode "BLUE";
_grp setBehaviour "CARELESS";
diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"halogroup.sqf","Airplane Created",[_vehicle, typeof _vehicle, _unit, typeof _unit]];
 // We set the crew captive so the vehicle is not a target as long as it is on the way to the player.
// As soon as the player get inside... the feast can begin :)
{
	_x setVariable ["BIS_noCoreConversations", true];
	_x setVariable ["zeu_AIBypass", true];
	_x setVariable ["asr_ai_sys_aiskill_exclude", true];
	_x setUnitAbility (0.9);
	_x setcaptive true;
	_x allowDamage false;
	_x disableAI "TARGET";
	_x disableAI "AUTOTARGET";
	_x disableAI "MOVE";
	_x disableAI "FSM";
	_x allowfleeing 0;
} foreach _crew;
_unit = driver _vehicle;
d_allunits_add set [count d_allunits_add, _vehicle];
d_allunits_add set [count d_allunits_add, _unit];
_vehicle lockdriver true;

{ deleteWaypoint _x;
} foreach waypoints _grp;

_wp = _grp addWaypoint [_dest, 0];
_grp setCurrentWaypoint _wp;
_unit doMove _dest;
_wp = _grp addWaypoint [_turn_pos, 0];
_wp = _grp addWaypoint [_dest, 0];
_wp = _grp addWaypoint [_spos, 0];
_vehicle flyinHeight _flying_height;

[_vehicle, _dest, (150 * (sin _cdir)), (120 * (cos _cdir)), (_flying_height / 3000)] spawn {
	private ["_vehicle","_dest_pos","_vel_x","_vel_y","_vx","_vy","_vz","_dx","_dy"];
	_vehicle = _this select 0;
	_dest_pos = _this select 1;
	_vel_x = _this select 2;
	_vel_y = _this select 3;
	_vel_z = _this select 4;
	while {  ([_vehicle, _dest_pos] call XfDistTo) > 150 } do {
		_vx = (velocity _vehicle) select 0;
		_vy = (velocity _vehicle) select 1;
		_vz = (velocity _vehicle) select 2;
		
		_dx = _vx - _vel_x;
		_dy = _vy - _vel_y;
		
		_vx = _vx - (_dx / 2);
		_vy = _vy - (_dy / 2);
		_vz = 0 - _vel_z;
		_vehicle setVelocity [_vx, _vy, _vz];
		sleep 0.02345;
	};
};

_cargo_pos = 2;
["d_all_exec", ["titleText ['','BLACK']; titleFadeOut 5; _units = _this select 1; _vehicle = _this select 2; if (player in _units) then { player assignAsCargo _vehicle; player setPos (getPos _vehicle); player moveinCargo [_vehicle, ((_units find player) + 2)];};",_flying_units, _vehicle]] call XNetCallEvent;
// {
	// _x allowDamage false;
	// _x assignAsCargo _vehicle;
	// _x setPos (getPos _vehicle);
	// _x moveinCargo [_vehicle, _cargo_pos];
	// _cargo_pos = _cargo_pos + 1;
	// _x allowDamage true;
// } foreach _flying_units;

waituntil { sleep 3.456; _unit doMove _dest; ([_vehicle, _dest] call XfDistTo) < 900};
["d_msg", ["STR_HALO_NEARLY_THERE","vec",_vehicle,"vehicle"]] call XNetCallEvent;
_light1 = "#lightpoint" createVehicleLocal [0,0,0];
_light1 setlightcolor [250,100,20];
_light1 setlightambient [0.4,0.4,0.1];
_light1 setlightbrightness 0.005;
_light1 attachTo [_vehicle,[0,-3,3]]; 

waituntil { sleep 0.123; ([_vehicle, _dest] call XfDistTo) < 400};
_light1 setlightcolor [100,250,20];
["d_msg", ["STR_HALO_GOGOGO","vec",_vehicle,"vehicle"]] call XNetCallEvent;
sleep 1.234;
["d_msg", ["STR_HALO_GOGOGO","vec",_vehicle,"vehicle"]] call XNetCallEvent;
sleep 1.234;
["d_msg", ["STR_HALO_GOGOGO","vec",_vehicle,"vehicle"]] call XNetCallEvent;
sleep 1.234;
{
	if (_x != driver _vehicle) then {
		if (!isplayer _x) then {
			_x action ["eject",_vehicle];
		} else {
			["d_all_exec",["_name = _this select 1; if (_name == str(player)) then {[] execVM 'client\halo.sqf';};",str(_x)]] call XNetCallEvent;
		};
	};
} foreach crew _vehicle;
_vehicle flyinHeight _flying_height;

waituntil { sleep 0.123; ([_vehicle, _dest] call XfDistTo) < 500};
{
	deleteVehicle _x;
} foreach crew _vehicle;
deleteVehicle _vehicle;
